package com.hejia.alauda.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.Announcement;
import com.hejia.alauda.entity.example.AnnouncementExample;
import com.hejia.alauda.persistence.mybatis.AnnouncementMapper;
import com.hejia.alauda.utils.Pager;
//import com.umpay.api.util.StringUtil;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

	@Resource
	private AnnouncementMapper announcementMapper;
	
	@Override
	public Pager<Announcement> findAnnouncements(Pager<Announcement> pager, String title) {
		
		AnnouncementExample example = new AnnouncementExample();
		if (StringUtils.isNotEmpty(title)) {
			example.appendCriterion("title like", "%" + title + "%");
		}
		example.setOrderByClause("id desc");
		pager.setList(this.announcementMapper.selectByExample(example, pager));
		pager.setTotalCount(this.announcementMapper.countByExample(example));
		
		return pager;
	}

	@Override
	public Announcement addAnnouncement(Announcement announcement) {
		if (StringUtils.isNotEmpty(announcement.getUrl())) {
			if (!announcement.getUrl().startsWith("http")) {
				throw new RuntimeException("URL地址格式错误");
			}
		}
		announcement.setCreateDate(new Date());
		announcement.setModifyDate(new Date());
		announcement.setPublishTime(new Date());
		this.announcementMapper.insert(announcement);
		return announcement;
	}

	@Override
	public void deleteAnnouncement(Long id) {
		this.announcementMapper.deleteByPrimaryKey(announcementMapper.selectByPrimaryKey(id));
	}

	@Override
	public Announcement findLatestAlertAnnouncement() {
		
		Pager<Announcement> pager = new Pager<>();
		pager.setPageSize(1);
		Date now = new Date();
		AnnouncementExample example = new AnnouncementExample();
		example.appendCriterion("alertExpireTime is not null");
		example.appendCriterion("publishTime is not null");
		example.setOrderByClause("publishTime desc");
		
		List<Announcement> announcements = this.announcementMapper.selectByExample(example, pager);
		if (announcements.size() > 0) {
			Announcement announcement = announcements.get(0);
			if (announcement.getExpireTime().before(now)) {
				return null;
			}
			if (announcement.getAlertExpireTime().before(now)) {
				return null;
			} 
			return announcement;
		}
		return null;
	}

	@Override
	public List<Announcement> findvalidAnnouncements() {
		
		Date now = new Date();
		AnnouncementExample example = new AnnouncementExample();
		example.appendCriterion("expireTime >", now);
		example.appendCriterion("publishTime is not null");
		example.setOrderByClause("publishTime desc");
		
		return this.announcementMapper.selectByExample(example, null);
	}

	@Override
	public Announcement findAnnouncementById(Long id) {
		return this.announcementMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateAnnouncement(Announcement announcement) {
		announcement.setModifyDate(new Date());
		this.announcementMapper.updateByPrimaryKey(announcement);
	}

}
